// pages/component/homes/transfer/transfer.js
var that;
var Util = require('../../../../utils/util.js');
var GlobalData = require('../../../../utils/GlobalData.js');
var dataManager = require('../../../../utils/dataManager.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputMoney: 0,
    income: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    this.setData({
      income: GlobalData.homeData.promotebalance
    });
    // console.log(this.data.income);
  },
  onInputMoney: function (e) {
    var money = parseInt(e.detail.value);
    if (money == 0) {
      return "";
    }
    this.setData({
      inputMoney: e.detail.value
    });

  },
  /**确定转账 */
  onPay: function () {
    that = this;
    var app = getApp();
    // console.log(that.data.inputMoney);
    if (that.data.inputMoney < 0.01 || isNaN(that.data.inputMoney)) {
      app.onTip("请输入正确的金额。");
      return;
    }
    if (that.data.inputMoney < 100) {
      app.onTip("转账的最小金额为100");
      return;
    }
    if (that.data.inputMoney > GlobalData.homeData.promotebalance) {
      app.onTip("可转账余额为 " + GlobalData.homeData.promotebalance + ", 不足 " + that.data.inputMoney);
      return;
    }
     let url = app.getRequestUrl(app.getConfig().isFormal);
    wx.request({
      url: url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: Util.json2Form({ method: "transfer", token: GlobalData.token, amount: this.data.inputMoney }),
      complete: function (res) {
        let pkg = res.data;
        // console.log(pkg);
        if (res && pkg && pkg.errcode == 0) {
          that.onComplete()
          // app.onTip("转账成功。", '../../homes/home/home', true);
        } else {
          console.error(pkg.errmsg);
          app.onTip(pkg.errmsg)
        }
      }
    })
  },

  /**转账成功 */
  onComplete: function () {
    that = this;
    var app = getApp();
     let url = app.getRequestUrl(app.getConfig().isFormal);
    wx.request({
      url: url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: Util.json2Form({ method: "getagent", token: GlobalData.token }),
      complete: function (res) {
        let pkg = res.data;
        // console.log(pkg);
        if (pkg && pkg.errcode == 0) {
          that.setData({
            income: pkg.data.promotebalance,

          });
          GlobalData.homeData = pkg.data
          if (dataManager.isClick("home")) {
            return
          } else {
            dataManager.btnObserver("home")
            app.onTip("转账成功。", '../../homes/home/home', true);
          }

          // console.log(that.data.income);
        } else {
          app.onTip(pkg.errmsg)
        }
        // console.log(res);
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    dataManager.removeBtnObserver("transfer")

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})